/*
 * Created on 04/12/2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package test.mushroomdb.transaction;

import org.mushroomdb.catalog.Catalog;
import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.engine.EvaluationEngineFactory;
import org.mushroomdb.exception.RDBMSRuntimeException;
import org.mushroomdb.query.Criteria;
import org.mushroomdb.query.impl.SelectionQuery;
import org.mushroomdb.query.impl.UpdateQuery;
import org.mushroomdb.security.SecurityService;
import org.mushroomdb.security.User;
import org.mushroomdb.service.ServiceManager;
import org.mushroomdb.transaction.TransactionService;

/**
 * @author Tomas
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TrxThread1 extends Thread {
    private Table table;
    
    public TrxThread1(){
        Catalog catalog = Catalog.getInstance();
        this.table = catalog.getTableByName("persona");
        
        
    }
    
    public void run() {
        User user = new User(new Long(2),"User2","User2");
        SecurityService securityService = (SecurityService) ServiceManager.getInstance().getService(SecurityService.class);
        securityService.setTestUser(user);
        
        TransactionService trxService = (TransactionService)ServiceManager.getInstance().getService(TransactionService.class);
        
        trxService.beginTransaction();
        
        Column nombre = this.table.getColumn("Nombre");
        Column apellido = this.table.getColumn("Apellido");
        Column edad = this.table.getColumn("Edad");

        SelectionQuery selectionQuery = new SelectionQuery();
//        selectionQuery.addProjection(nombre);
//        selectionQuery.addProjection(edad);

        Criteria criteria = selectionQuery.getNewCriteria();
        criteria.addEqualsTo(nombre, "Tomas");

        UpdateQuery updateQuery = new UpdateQuery(selectionQuery);

        updateQuery.setUpdate(apellido, "H");

        int updateCount = EvaluationEngineFactory.getInstance().executeUpdate(
                updateQuery);
        if (updateCount != 1) {
            throw new RDBMSRuntimeException();
        }
        
        SelectionQuery selectionQuery2 = new SelectionQuery();
//      selectionQuery.addProjection(nombre);
//      selectionQuery.addProjection(edad);

      Criteria criteria2 = selectionQuery2.getNewCriteria();
      criteria2.addEqualsTo(nombre, "Tomas");

      UpdateQuery updateQuery2 = new UpdateQuery(selectionQuery2);

      updateQuery2.setUpdate(apellido, "HENSE");

      int updateCount2 = EvaluationEngineFactory.getInstance().executeUpdate(
              updateQuery2);
      if (updateCount2 != 1) {
          throw new RDBMSRuntimeException();
      }
      
      trxService.commitTransaction();
    }
    

}
